<template>
  <div style="display: inline-block;margin-right: 5px;">
    <Modal v-model="modal1"
           title="编辑"
           ok-text="提交"
           :loading="loading"
           @on-ok="handleSubmit('formValidate')">
      <Form ref="formValidate"
            class="form"
            :model="formValidate"
            :label-width="60">
        <FormItem label="分类"
                  class="form-item"
                  prop="ClassName">
          <Input v-model="formValidate.ClassName"
                 placeholder="请输入您的分类" />
        </FormItem>
        <FormItem label="Key值"
                  class="form-item"
                  prop="OrdKey">
          <Select v-model="formValidate.OrdKey"
                  multiple>
            <Option v-for="item in OrdKey"
                    :value="item"
                    :key="item">{{ item }}</Option>
          </Select>
        </FormItem>
      </Form>
    </Modal>
  </div>
</template>

<script>
import { getGoodsAttrDetail, editGoodsAttrDetail } from '@/api/goods'

export default {
  name: 'edit',
  data () {
    return {
      modal1: false,
      loading: true,
      formValidate: {},
      OrdKey: [],
      currentColId: ''
    }
  },
  mounted () {
    this.init()
  },
  methods: {
    init () {
      this.modal1 = false
      this.loading = true
      this.formValidate = {}
      this.OrdKey = []
      this.editData = {}
      this.handleReset('formValidate')
    },
    getItem (id) {
      this.handleReset('formValidate')
      this.currentColId = id
      getGoodsAttrDetail({ AutoID: id }).then(res => {
        if (res.data.success) {
          this.formValidate = res.data.data
          this.OrdKey = res.data.data.OrdKey.split(',')
          this.formValidate.OrdKey = this.OrdKey
        } else {
          this.$Message.info(res.data.msg)
        }
      }).then(() => {
        this.modal1 = true
      })
    },
    handleSubmit (name) {
      this.formValidate.OrdKey = this.formValidate.OrdKey.join(',')
      this.formValidate.SysUserId = localStorage.getItem('userId')
      this.formValidate.UserID = this.currentColId
      editGoodsAttrDetail(this.formValidate).then(res => {
        if (res.data.success) {
          this.modal1 = false
          this.loading = true
          this.$Message.info('编辑成功!')
        } else {
          this.modal1 = true
          this.loading = false
          this.$Message.info('编辑失败!')
        }
      }).then(() => {
        this.$emit('getList')
      })
    },
    // 重置表单
    handleReset (name) {
      this.$refs[name].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
</style>
